package com.cloudptt.wp.ui.address.map;

import com.cloudptt.wp.R;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class CompassView extends View {
	private Drawable mCompass;
	private float mAzimuth = 0;
	private boolean mSideBottom = true;
	private int PADDING = 2;
	private int width;
	private int height;

	
	
	public CompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mCompass = context.getResources().getDrawable(R.drawable.compass);
		BitmapFactory.Options options = new BitmapFactory.Options();
		BitmapFactory.decodeResource(getResources(), R.drawable.compass,options);
		width = options.outWidth+PADDING;;
		height = options.outHeight+PADDING;;
	}

	public CompassView(Context ctx, final boolean sideBottom) {
		super(ctx);
		this.mCompass = ctx.getResources().getDrawable(R.drawable.compass);
		mSideBottom = sideBottom;
		BitmapFactory.Options options = new BitmapFactory.Options();
		BitmapFactory.decodeResource(getResources(), R.drawable.compass,options);
		width = options.outWidth+PADDING;
		height = options.outHeight+PADDING;
	}
	public void onMeasure(int widthMeasure, int heightMeasure) {
		setMeasuredDimension(width, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		if (mSideBottom) {
			canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth() / 2,
					PADDING + mCompass.getMinimumHeight() / 2);
			mCompass.setBounds(PADDING, PADDING, PADDING + mCompass.getMinimumWidth(),
					PADDING + mCompass.getMinimumHeight());
		} else {
			canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth() / 2,
					this.getHeight() - mCompass.getMinimumHeight() / 2 - PADDING);
			mCompass.setBounds(PADDING, this.getHeight() - mCompass.getMinimumHeight() - PADDING,
					PADDING + mCompass.getMinimumWidth(), this.getHeight() - PADDING);
		}
		mCompass.draw(canvas);
		canvas.restore();

		super.onDraw(canvas);
	}

	public void setAzimuth(float aAzimuth) {
		mAzimuth = aAzimuth;
	}

}
